/*******************************发送分页查询ajax请求******************************/
var currentPage = 1; //当前页
var pageSize = 5; //每页显示条数
var totalCount = 0; //总记录条数
var totalPage = 0; //总页数

function PageFind() {
    //1.获取输入框上的值
    var startTime = $('#startTime').val();
    var endTime = $('#endTime').val();
    //2.发送ajax 
    $.ajax({
        url: "adminLog/findAdminLogByPage",
        type: "post",
        dataType: "JSON",
        data: {
            startTime: startTime,
            endTime: endTime,
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function(pageBean) {
            //设置参数
            totalCount = pageBean.totalCount;
            totalPage = parseInt(pageBean.totalPage);
            //数据插入函数
            showTableData(pageBean.list);
        }
    })
};
//发送ajax
PageFind();

function showTableData(dataList) {
    //1.设置当前页和总页数
    var pageNumberData = currentPage + '/' + totalPage;
    $('#pageNumber').html(pageNumberData);
    //2.设置表格数据
    //2.1清空表格
    $('#tableData').html('');
    //2.2遍历数据
    var trs = '';
    for (var i = 0; i < dataList.length; i++) {
        var dataDetail = dataList[i];
        //改日期
        var cDate = new Date(dataDetail.doCtime).format('yyyy-MM-dd hh:mm:ss');
        var tr = `
            <tr>
                <td hidden="hidden">${dataDetail.alogId}</td>
                <td hidden="hidden">${dataDetail.adminId}</td>
                <td>${dataDetail.adminName}</td>
                <td>${dataDetail.doName}</td>
                <td>${cDate}</td>
                <td>
                    <button class="btn btn-mini " type="button" id='deleteBtn'>删除</button>
                </td>
            </tr>      
        `;
        //添加数据
        trs += tr;
    }
    //2.3将数据加入
    $('#tableData').html(trs);
    //3.删除操作
    $('[id = deleteBtn]').click(function() {
        //1.获取日志Id
        var logId = $(this).parent().parent().find('td').eq(0).html();
        var logIdList = [logId];
        layer.confirm('确定要删除记录日志吗？', {
            btn: ['确定', '取消'] //按钮
        }, function() {
            deleteOperation(logIdList);
        }, function() {
            console.log('取消删除');
        });
    })
};
//上一页 下一页按钮 搜索 重置 触发事件
$('#prePageBtn').click(function() {
    currentPage -= 1;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    PageFind();
});
$('#nextPageBtn').click(function() {
    currentPage += 1;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    PageFind();
});
$('#searchBtn').click(function() {
    PageFind();
});
$('#reSetBtn').click(function() {
    $('#startTime').val('');
    $('#endTime').val('');
});
/* *******************删除操作****************** */
function deleteOperation(logIdList) {

    //1.发送ajax
    $.ajax({
        url: "adminLog/deleteLog",
        type: "post",
        dataType: "JSON",
        data: {
            logIdList: logIdList
        },
        success: function(data) {
            if (data > 0) {
                layer.confirm('删除成功', {
                    btn: ['确定'] //按钮
                });
                PageFind();
            }
        }
    });
}